<?php
/**
* 后台控制器
*/
class AdminController extends Controller{
	public function index(){
		$m=M('User');
		$name = cookie('name');
		$where['name']=$name;
		$data=$m->field('role')->where($where)->select();
		$this->assign('role',$data[0]['role']);
		$m=M('Root');
		$rootname['rootname']=$data[0]['role'];
		$data1=$m->field(true)->where($rootname)->select();
		$this->assign('root',$data1);
		$this->assign('title',"康冠智库后台");
		$this->display();
	}
	public function add(){
		$m=M('User');
		if($_POST['type']=='update'){
			$name['name']=$_POST['name'];
			$rygl['username']=$_POST['username'];
			$rygl['password']=$_POST['password'];
			$rygl['role']=$_POST['role'];
			$m->where($name)->save($rygl);
		}else{
			$rygl['name']=$_POST['name'];
			$rygl['username']=$_POST['username'];
			$rygl['password']=$_POST['password'];
			$rygl['role']=$_POST['role'];
			$m->data($rygl)->add();
		}
	}
	public function find(){
		$m=M('User');
		$username=$_POST['username'];
		$name=$_POST['name'];
		if($name==""){
			 $data=$m->where("username=$username")->count();
		}else{
			$data=$m->where("name=$name")->count();
		}
		if($data!=0){
			$res = array('status'=>1,'count'=>$data);
			$this->ajaxReturn($res);
		}else{
			$res = array('status'=>0,'count'=>0);
			$this->ajaxReturn($res);
		}

	}
	public function delete(){
		$m=M('User');
		$data=$_REQUEST['name'];
		$delete['name']=array('in',$data); 
		$m->where($delete)->delete();
	}
	public function alldelete(){
		$m=M('Root');
		$data=$_REQUEST['rootname'];
		$delete['rootname']=array('in',$data); 
		$m->where($delete)->delete();
	}
	public function allbzdelete(){
		$m=M('Bzgl');
		$data=$_REQUEST['bzname'];
		$bzname['bzname']=array('in',$data); 
		$m->where($bzname)->delete();
	}
	public function rootdelete(){
		$m=M('Root');
		$rootname['rootname']=$_POST['rootname'];
		$m->where($rootname)->delete();	
	}
	public function bzssdelete(){
		$m=M('Bzgl');
		$bzname['bzname']=$_POST['bzname'];
		$m->where($bzname)->delete();	
	}
	public function rootupdate(){
		$m=M('Root');
		$rootname['rootname']=$_POST['rootname'];
		$data=$m->where($rootname)->select();
		if($data!=""){
			$data['type']="update";
			$this->ajaxReturn($data);
		}else{
			$res = array('count'=>0);
			$data['type']="add";
			$this->ajaxReturn($res);
		}
	}
	public function ryglupdate(){
		$m=M('User');
		$name['name']=$_POST['name'];
		$data=$m->where($name)->select();
		if($data!=""){
			$data['type']="update";
			$this->ajaxReturn($data);
		}else{
			$res = array('count'=>0);
			$data['type']="add";
			$this->ajaxReturn($res);
		}
	}
	public function bzssupdate(){
		$m=M('Bzgl');
		$bzname['bzname']=$_POST['bzname'];
		$data=$m->where($bzname)->select();
		if($data!=""){
			$data['type']="update";
			$this->ajaxReturn($data);
		}else{
			$res = array('count'=>0);
			$data['type']="add";
			$this->ajaxReturn($res);
		}
	}
	public function bzzsadd(){
		$m=M('Bzgl');
		$date=date('Y-m-d H:i:s');
		if($_POST['type']=='update'){
			$bzss['updatetime']=$date;
			$name['bzname']=$_POST['bzname'];
			$bzss['sort']=$_POST['sort1'];
			$bzss['level']=$_POST['level'];
			$bzss['pid']=$_POST['pid'];
			$m->where($name)->save($bzss);
		}else{
			$bzss['addtime']=$date;
			$bzss['bzname']=$_POST['bzname'];
			$bzss['sort']=$_POST['sort1'];
			$bzss['level']=$_POST['level'];
			$bzss['pid']=$_POST['pid'];
			$m->data($bzss)->add();
		}
	}
	public function rygl(){
		$m1=M('Root');
		$rootname=$m1->field("rootname")->select();
		$m=M('User');
		foreach($rootname as $key=>$value){
			$where=$value['rootname'];
			$admin=$m->where("role='$where'")->select();
			$rootname[$key]['role']=$admin;
		}
		$this->assign('admin',$rootname);
		$this->display();
	}
	public function root(){
		$m=M('Root');
		$date=date('Y-m-d H:i:s');
		if($_POST['type']=='update'){
			$root['updatetime']=$date;
			$name['rootname']=$_POST['rootname'];
			$root['bqgl']=$_POST['bqgl'];
			$root['bzgl']=$_POST['bzgl'];
			$root['chzk']=$_POST['chzk'];
			$root['jbbk']=$_POST['jbbk'];
			$root['jjzk']=$_POST['jjzk'];
			$root['qxgl']=$_POST['qxgl'];
			$root['rygl']=$_POST['rygl'];
			$root['zxzk']=$_POST['zxzk'];
			$m->where($name)->save($root);
		}else{
			$root['addtime']=$date;
			$root['rootname']=$_POST['rootname'];
			$root['bqgl']=$_POST['bqgl'];
			$root['bzgl']=$_POST['bzgl'];
			$root['chzk']=$_POST['chzk'];
			$root['jbbk']=$_POST['jbbk'];
			$root['jjzk']=$_POST['jjzk'];
			$root['qxgl']=$_POST['qxgl'];
			$root['rygl']=$_POST['rygl'];
			$root['zxzk']=$_POST['zxzk'];
			$m->data($root)->add();
		}
	}
	public function qxgl(){
		import("Org.Page");
		$m=M('Root');
		$count=$m->count();
		$this->assign('count',$count);
		$Page = new Page($count,8);
		$show = $Page->show();
        $list = $m->field(true)->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page',$show); // 赋值分页输出
		$this->display();
	}
	public function bzss(){
		import("Org.Page");
		$m=M('Bzgl');
		$count=$m->count();
		$this->assign('count',$count);
		$Page = new Page($count,8);
		$show = $Page->show();
		$list = $m->field(true)->order('pid,sort')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page',$show); // 赋值分页输出
		$this->display();
	}
}